﻿Imports System.Globalization
Partial Class WebForm_ManageFinancePercentageFinance_FinancePercentage
    Inherits System.Web.UI.UserControl
#Region "事件"
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack() Then
            Me.BindDDLDivision()
            Me.BindDDLSignature()
            Me.BinDDLByMonth()
        End If
    End Sub

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Me.Signature = Me.ddlSignature.SelectedValue
        Me.plList.Visible = True

        For i As Integer = 0 To cblCheck.Items.Count - 1
            Me.cblCheck.Items(i).Selected = False
        Next

        If FixedData.Rows.Count > 0 Then
            If Me.BySecondLevel = "Y" Then
                MyObj.MatchItem(Me.cblCheck, "BySecondLevel")
            End If
            If Me.ByChannel = "Y" Then
                MyObj.MatchItem(Me.cblCheck, "ByChannel")
            End If

            MyObj.MatchItem(Me.ddlByMonth, Me.LimitMonth)
        End If
    End Sub
    Protected Sub ddlDivision_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDivision.SelectedIndexChanged
        Me.BindDDLSignature()
    End Sub

    Protected Sub btnApply_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnApply.Click
        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        Dim CheckCount As Integer = 0
        sql.AppendFormat("Delete From TCReportFixedRule ")
        sql.AppendFormat("Where Signature ='{0}' ;", Me.Signature)

        Dim BySecondLevel As String = "N"
        Dim ByChannel As String = "N"
        For Each li As ListItem In Me.cblCheck.Items
            If li.Selected Then
                If li.Value = "BySecondLevel" Then BySecondLevel = "Y"
                If li.Value = "ByChannel" Then ByChannel = "Y"
                CheckCount += 1
            End If
        Next

        sql.AppendFormat("Insert TCReportFixedRule (Signature,BySecondLevel,ByChannel,LimitMonth)")
        sql.AppendFormat("Select '{0}' ", Me.Signature)
        sql.AppendFormat(",'{0}' ", BySecondLevel)
        sql.AppendFormat(",'{0}' ", ByChannel)
        sql.AppendFormat(",'{0}'; ", Me.ddlByMonth.SelectedValue)

        'If CheckCount = 2 Then
        '    MyObj.ShowMessage("It can not choose two options.")
        '    Exit Sub
        'End If

        Try
            dat.AccData(sql)
            MyObj.ShowMessage("Apply Successful!")
        Catch ex As Exception
            MyObj.WriteLog(sql.ToString())
            MyObj.ShowMessage("Apply Failed!")
        End Try
    End Sub

#End Region

#Region "繫結資料"
    Private Sub BindDDLDivision()
        Dim ddlControl As DropDownList = Me.ddlDivision
        Dim dvManager As New DivisionManager

        ddlControl.DataTextField = "Division"
        ddlControl.DataValueField = "Division"
        ddlControl.DataSource = dvManager.GetAllDivision()
        ddlControl.DataBind()
    End Sub
    Private Sub BindDDLSignature()
        Dim ddlControl As DropDownList = Me.ddlSignature
        Dim stManager As New SignatureManager

        ddlControl.DataTextField = "Description"
        ddlControl.DataValueField = "Signature"
        ddlControl.DataSource = stManager.GetSignatureNoNewCategories(Me.ddlDivision.SelectedValue)
        ddlControl.DataBind()
    End Sub
   
    Private Sub BinDDLByMonth()
        Dim ddlControl As DropDownList = Me.ddlByMonth
        ddlControl.Items.Clear()

        For i As Integer = 0 To 6
            ddlControl.Items.Add(i)
        Next
        MyObj.MatchItem(ddlControl, 0)
    End Sub
#End Region

#Region "屬性"

    Private _FixedData As DataTable
    Private BySecondLevel, ByChannel As String
    Private LimitMonth As Integer
    Protected ReadOnly Property FixedData() As DataTable
        Get
            If Me._FixedData Is Nothing Then
                Dim sql As New MyStringBuilder
                Dim dat As New clsData

                sql.Length = 0
                sql.AppendFormat("select BySecondLevel,ByChannel,LimitMonth from TCReportFixedRule ")
                sql.AppendFormat("Where Signature='{0}' ", ddlSignature.SelectedValue)
                Dim dt As DataTable = dat.GetData(sql.ToString)
                Me._FixedData = dt
                If Me._FixedData.Rows.Count = 0 Then
                    Me.BySecondLevel = "N"
                    Me.ByChannel = "N"
                    Me.LimitMonth = 0
                Else
                    Me.BySecondLevel = Me._FixedData.Rows(0)("BySecondLevel").ToString()
                    Me.ByChannel = Me._FixedData.Rows(0)("ByChannel").ToString()
                    Me.LimitMonth = Me._FixedData.Rows(0)("LimitMonth").ToString()
                End If
            End If
            Return Me._FixedData
        End Get
    End Property

    Public Property Signature() As String
        Get
            Return ViewState("Signature")
        End Get
        Set(ByVal value As String)
            ViewState("Signature") = value
        End Set
    End Property
#End Region

End Class
